package com.rc.evcharger.task;

import com.github.pagehelper.util.StringUtil;
import com.rc.evcharger.common.base.BaseTask;
import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.common.utils.DataUtil;
import com.rc.evcharger.common.utils.TimeoutUtil;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.redis.RedChargeDataService;
import com.rc.evcharger.service.order.OrderInfoService;
import com.rc.evcharger.vo.charge.StopChargeReq;
import com.rc.evcharger.client.ChargerClientRouter;
import io.netty.util.Timeout;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class ElecFullTask extends SupperTask implements BaseTask {
    private static Logger logger = LogManager.getLogger(ElecFullTask.class);

    public final int TASKSLEPPE_MINUTES = 5;

    @Autowired
    private OrderInfoService orderService;

    @Autowired
    private ChargerClientRouter chargerClientRouter;

    @Autowired
    private RedChargeDataService redChargeDataService;


    @Override
    public void runTask(Timeout timeout) throws Exception {
        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
        String key = timeoutUtil.getRtua(timeout);

        timeout.cancel();
        timeoutUtil.removeTimeout(key);
//        logger.warn("订单充满结算定时器开始执行: 执行的key为：" + key);
        if (key == null) {
            return;
        }
        //key = cc\messcat\task\ElecFullTask19 00 14 01 01 001520099452500001200
        key = key.replace(this.getClass().getName(), "");

        int index = key.lastIndexOf(" ");
        String orderNum = key.substring(index + 1);


        OrderInfo order = orderService.selectByOrderNum(orderNum);
        if (order == null) {
            logger.error("发送充满自动停止指令出错，该订单号不存在:{}", orderNum);
            return;
        }
        StopChargeReq stopChargeRequest = new StopChargeReq();
        stopChargeRequest.setOrderId(order.getOrderId());
        stopChargeRequest.setOrderNum(order.getOrderNum());
        stopChargeRequest.setStopType(SocketConstant.STOP_ELEC_FULL_0x9);
        stopChargeRequest.setElecPileId(order.getElecPileId());

        /**发送充满自动停止指令**/
        //commandService.query_stop_charge(stopChargeRequest);
        //远程调用停止指令
        chargerClientRouter.query_stop_charge(stopChargeRequest);

        logger.warn("订单充满结算定时器:订单号为：" + orderNum);
    }

    @Override
    public void removeTask(String rtua) {
        //rtua = orderStopReason(rtua, SocketConstant.CON0 + "");

        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
        Timeout timeout = timeoutUtil.getTimeout(this.getClass().getName() + rtua);
        if (timeout != null) {
            timeout.cancel();
            timeoutUtil.removeTimeout(this.getClass().getName() + rtua);
            logger.warn("充满结算定时器被删除:{}", rtua);
        }
    }

    @Override
    public void addTask(String rtua, String gunNum, String orderNum, Integer time, TimeUnit tu) {
        TimeUnit timeUnit = TimeUnit.MINUTES;
        if (tu != null) {
            timeUnit = tu;
        }
        if (time == null) {
            time = this.MINUTE;
        }

        //是否在充电中的订单号
        String orderNumInfo = redChargeDataService.getOrderNum(rtua, gunNum, orderNum);
        if (StringUtil.isEmpty(orderNumInfo)) {
            return;
        }
        //从redis里根据
        //if (redOrderService.hasOrder(rtua, gunNum, null)) {
        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
        Timeout timeout = timer.newTimeout(this, time, timeUnit);
        String key = this.getClass().getName() + DataUtil.getKey(rtua, gunNum, orderNum);
        logger.warn("添加充满结算定时器key" + key);
        timeoutUtil.addTimeout(key, timeout);
        //}
    }

    public boolean hasTask(String rtua, String gunNum, String orderNum) {
        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
        String key = this.getClass().getName() + DataUtil.getKey(rtua, gunNum, orderNum);
        Timeout timeout = timeoutUtil.getTimeout(key);
        if (timeout != null) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String aa = "19 00 14 01 01 001520099452500001200";
        int i = aa.lastIndexOf(" ");
        System.out.println(aa.substring(i + 1));
    }


//    @Override
//    public void runTask(Timeout timeout) throws Exception {
//        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
//        String key = timeoutUtil.getRtua(timeout);
////        logger.warn("订单充满结算定时器开始执行: 执行的key为：" + key);
//        if (key == null) {
//            return;
//        }
//        //key = cc\messcat\task\ElecFullTask19 00 14 01 01 001520099452500001200
//        key = key.replace(this.getClass().getName(), "");
//
//        int index = key.lastIndexOf(" ");
//        String orderNum = key.substring(index + 1);
//
//
//        OrderInfo order = orderService.selectByOrderNum(orderNum);
//        if (order == null) {
//            logger.error("发送充满自动停止指令出错，该订单号不存在:{}", orderNum);
//            return;
//        }
//        StopChargeReq stopChargeRequest = new StopChargeReq();
//        stopChargeRequest.setOrderId(order.getOrderId());
//        stopChargeRequest.setOrderNum(order.getOrderNum());
//        stopChargeRequest.setStopType(SocketConstant.STOP_ELEC_FULL_0x9);
//        stopChargeRequest.setElecPileId(order.getElecPileId());
//
//        /**发送充满自动停止指令**/
//        //commandService.query_stop_charge(stopChargeRequest);
//        //远程调用停止指令
//        chargerClientRouter.query_stop_charge(stopChargeRequest);
//
//        logger.warn("订单充满结算定时器:订单号为：" + orderNum);
//    }
//
//    //rtua =19 00 14 01 01 001520099452500001200
//    @Override
//    public void removeTask(String rtua) {
//        rtua = orderStopReason(rtua, SocketConstant.CON0 + "");
//
//        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
//        Timeout timeout = timeoutUtil.getTimeout(this.getClass().getName() + rtua);
//        if (timeout != null) {
//            timeout.cancel();
//            timeoutUtil.removeTimeout(this.getClass().getName() + rtua);
//            logger.warn("充满结算定时器被删除" + this.getClass().getName() + rtua);
//        }
//    }
//
//    @Override
//    public void addTask(String rtua, String gunNum, String orderNum, Integer time, TimeUnit tu) {
//        TimeUnit timeUnit = TimeUnit.MINUTES;
//        if (tu != null) {
//            timeUnit = tu;
//        }
//        if (time == null) {
//            time = this.MINUTE;
//        }
//        //从redis里根据
//        if (redOrderService.hasOrder(rtua, gunNum, null)) {
//            TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
//            Timeout timeout = timer.newTimeout(this, time, timeUnit);
//            String key = DataUtil.getKey(rtua, gunNum, orderNum);
//            logger.warn("添加充满结算定时器key" + this.getClass().getName() + key);
//            timeoutUtil.addTimeout(this.getClass().getName() + key, timeout);
//        }
//    }
//
//    public boolean hasTask(String rtua, String gunNum, String orderNum) {
//        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
//        String key = DataUtil.getKey(rtua, gunNum, orderNum);
//        Timeout timeout = timeoutUtil.getTimeout(this.getClass().getName() + key);
//        if (timeout != null) {
//            return true;
//        }
//        return false;
//    }
//
//    public static void main(String[] args) {
//        String aa = "19 00 14 01 01 001520099452500001200";
//        int i = aa.lastIndexOf(" ");
//        System.out.println(aa.substring(i + 1));
//    }
}
